/*~-1*/
/*~XSF_LANGUAGE: C/C++*/
/*~A:1*/
/*~+:File Header*/
/*~T*/
/****************************************************************************
 * 
 ****************************************************************************/

/*~E:A1*/
/*~A:2*/
/*~+:Import*/
/*~T*/
/* Import                                                                      */

/*~T*/
/* generic definitions */
#include <general.h>

/*~T*/
#include <avr/io.h>
#include <avr/pgmspace.h> 

/*~T*/
/* own header file */
#include <bsl_com.h>

/*~T*/

/*~E:A2*/
/*~A:3*/
/*~+:Private Macro Definitions*/
/*~T*/
/* Private Macro Definitions                                                   */

/*~T*/

/*~E:A3*/
/*~A:4*/
/*~+:Private Variable Definitions */
/*~T*/
/* Private Variable Definitions                                                */

/*~T*/

/*~E:A4*/
/*~A:5*/
/*~+:Public Functions*/
/*~T*/
/* Public Functions                                                            */

/*~A:6*/
/*~+:void com_init()*/
/*~F:7*/
void com_init(void)
/*~-1*/
{
   /*~T*/
   /* set baud rate to 38400(25) 19200(51)*/
   UBRRH = 0;
   UBRRL = 25;

   /*~T*/
   /* enable RX and TX */
   UCSRB = (1<<RXEN)|(1<<TXEN);

   /*~T*/
   /* set frame format:  8N1 */
   UCSRC = (1<<URSEL)|(1<<UCSZ1)|(1<<UCSZ0);

   /*~T*/

/*~-1*/
}
/*~E:F7*/
/*~E:A6*/
/*~A:8*/
/*~+:u8 com_send(u8 *buf, u8 count, u8 response)*/
/*~F:9*/
u8 com_send(const u8 *buf, u8 count, u8 response)
/*~-1*/
{
   /*~A:10*/
   /*~+:local variables*/
   /*~T*/
   u8 i;
   u16 timeout;

   /*~E:A10*/
   /*~T*/

   /*~A:11*/
   /*~+:transmit the data*/
   /*~L:12*/
   for (i=0;i<count;i++)
   /*~-1*/
   {
      /*~L:13*/
      /* wait for end of a previous transmission */
      while (!(UCSRA & (1<<UDRE)))
      /*~-1*/
      {
         /*~T*/

      /*~-1*/
      }
      /*~E:L13*/
      /*~T*/
      /* transmit the current byte */
      UDR = pgm_read_byte(&buf[i]);

   /*~-1*/
   }
   /*~E:L12*/
   /*~T*/

   /*~L:14*/
   while (UCSRA & (1<<RXC))
   /*~-1*/
   {
      /*~T*/
      i = UDR;

   /*~-1*/
   }
   /*~E:L14*/
   /*~T*/

   /*~E:A11*/
   /*~A:15*/
   /*~+:wait for a response*/
   /*~T*/
   timeout = 0;

   /*~T*/
   /* wait for a received byte */

   /*~I:16*/
   if (response==0xD5)
   /*~-1*/
   {
      /*~T*/
      while (!(UCSRA & (1<<RXC))){};
      i=UDR;

   /*~-1*/
   }
   /*~O:I16*/
   /*~-2*/
   else
   {
      /*~T*/
      while (!(UCSRA & (1<<RXC))){};
      i=UDR;

      while (!(UCSRA & (1<<RXC))){};
      i=UDR;

   /*~-1*/
   }
   /*~E:I16*/
   /*~L:17*/
   while ((!(UCSRA & (1<<RXC))) && (timeout < BSL_TIMEOUT))
   /*~-1*/
   {
      /*~T*/
      timeout += 1;

   /*~-1*/
   }
   /*~E:L17*/
   /*~I:18*/
   if (timeout < BSL_TIMEOUT)
   /*~-1*/
   {
      /*~T*/
      /* get the received data from buffer */
      i = UDR;

      /*~I:19*/
      if (i == response)
      /*~-1*/
      {
         /*~T*/
         /* response ok */
         return COM_OK; 
      /*~-1*/
      }
      /*~O:I19*/
      /*~-2*/
      else
      {
         /*~T*/
         /* return error */
         return COM_ERROR; 
      /*~-1*/
      }
      /*~E:I19*/
   /*~-1*/
   }
   /*~O:I18*/
   /*~-2*/
   else
   {
      /*~T*/
      /* return timeout error */
      return COM_ERROR; 
   /*~-1*/
   }
   /*~E:I18*/
   /*~T*/

   /*~E:A15*/
   /*~T*/

/*~-1*/
}
/*~E:F9*/
/*~E:A8*/
/*~T*/

/*~E:A5*/
/*~T*/

